package com.qiyu.mq.core.config;

import com.qiyu.mq.core.consumer.DefaultConsumerFactory;
import com.qiyu.mq.core.producer.DefaultProducerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author  idea
 * @version V1.0
 * @date 2020/8/10
 */
@Configuration
@EnableConfigurationProperties(RocketMQProperties.class)
public class RocketMqAutoConfiguration {

    @Resource
    private RocketMQProperties rocketMQProperties;

    @Bean
    @ConditionalOnMissingBean
    public DefaultConsumerFactory consumerFactory() {
        DefaultConsumerFactory defaultConsumerFactory = new DefaultConsumerFactory(rocketMQProperties.getNameServers(), rocketMQProperties.getMqConsumerConfig(), "test");
        return defaultConsumerFactory;
    }

    @Bean
    @ConditionalOnMissingBean
    public DefaultProducerFactory producerFactory(){
        DefaultProducerFactory defaultProducerFactory = new DefaultProducerFactory(rocketMQProperties.getNameServers(),rocketMQProperties.getMqProducerConfig());
        return defaultProducerFactory;
    }
}
